/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.driving.core.base.BaseController;
import com.driving.core.base.IPersonController;
import com.driving.core.entity.Admin;
import com.driving.core.mapper.type.Gender;
import com.driving.core.service.IAdminService;
import com.driving.core.service.dto.AdminDTO;
import com.driving.core.service.dto.CoachDTO;
import com.driving.core.service.dto.SchoolDTO;
import com.driving.core.util.Constant;
import com.driving.core.util.MessageType.ExceptionType;
import com.driving.core.util.Response;

/**
 * @since 2017
 */
@RestController
@RequestMapping(value = "/admin")
public class AdminController extends BaseController<IAdminService, Admin> implements IPersonController<AdminDTO> {

    @Override
    @PostMapping(value = "/login")
    public Response<Map<String, String>> login(@RequestBody AdminDTO adminDTO) {
        if (!validateAllParams(adminDTO.getMobile(), adminDTO.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.login(adminDTO));
    }

    @Override
    @PostMapping(value = "/password-update")
    public Response<Boolean> passwordUpdate(HttpServletRequest request, @RequestBody AdminDTO adminDTO) {
        if (validateAllParams(adminDTO.getOldPassword(), adminDTO.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        String token = request.getHeader(Constant.TOKEN);
        return Response.success(baseService.passwordUpdateOrReset(adminDTO, token));
    }

    @Override
    @PostMapping(value = "/password-reset")
    public Response<Boolean> passwordReset(@RequestBody AdminDTO dto) {
        if (validateAllParams(dto.getMobile(), dto.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.passwordUpdateOrReset(dto, null));
    }

    @PostMapping(value = "/add-coach")
    public Response<Map<String, Object>> addCoach(@RequestBody CoachDTO coachDTO) {
        String name = coachDTO.getName();
        Gender gender = coachDTO.getGender();
        String mobile = coachDTO.getMobile();
        String correctType = coachDTO.getCorrectType();
        String password = coachDTO.getPassword();
        if (!validateAllParams(name, gender, mobile, correctType, password)) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.addCoach(coachDTO));
    }

    @PostMapping(value = "/add-school")
    public Response<Map<String, Object>> addSchool(@RequestBody SchoolDTO schoolDTO) {
        if (!validateAllParams(schoolDTO.getName(), schoolDTO.getDetail())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.addSchool(schoolDTO));
    }
}
